#!/bin/bash

exp=$1
lang=$2
size=$3
if [ -z "$size" -o -z "$lang" ] ; then
	echo "Usage: $0 <name> <lang> <size> [-test] [ <options> ]"
	exit 1
fi
exp=`echo $exp | sed -e 's/^.*\/\([^/]*\)/\1/g' | sed -e 's/exp-//g'`
echo "exp=$exp lang=$lang size=$size"
shift
shift
shift

# Check whether we should use final test file or extract development
# set from training set (default)
type="dev"
if [ "$1" = "-test" ] ; then
	type="test"
	shift
fi
options=$*

# Default java options
javaopts=$JAVAOPTS
if [ -z "$javaopts" ] ; then
	if [ -f .javaopts ] ; then
		javaopts=`cat .javaopts`
	else
		if [ $size -gt 250 ] ; then
			xmx=30
		elif [ $size -gt 200 ] ; then
			xmx=20
		elif [ $size -gt 100 ] ; then
			xmx=10
		elif [ $size -gt 50 ] ; then
			xmx=8
		elif [ $size -gt 20 ] ; then
			xmx=5
		else
			xmx=2
		fi
		javaopts="-Xmx${xmx}000m"
	fi
fi

# Find training data
conll=./conll/data
trainsrc=`find $conll/$lang -name '*train*.conll' | head -1`
mkdir -p experiments/results

# Abort if we cannot find training data
if [ -z "$trainsrc" ] ; then
	echo "Could not find training file $conll/$lang/.../*train*.conll"
	exit 1
fi

# Create test data
test=`find $conll/$lang -name '*gs.conll' | head -1`
if [ -z "$test" ] ; then
	echo "Could not find test file $conll/$lang/.../*gs.conll"
	exit 1
fi

# Split off test data from training data unless we are testing
trainlength=`wc -l $trainsrc`
traindev=/tmp/traindev.$$
if [ "$type" != "test" ] ; then
	test="/tmp/test.$$"
	testsize=$TESTSIZE
	if [ -z "$testsize" ] ; then testsize=20; fi
	testoffset=$TESTOFFSET
	if [ -z "$testoffset" ] ; then testoffset=0; fi
	testoffset=`echo "$testoffset * ($trainlength - $testsize) | bc -l"`

	cat $trainsrc | experiments/subcorpus $testoffset $testsize > $test
	cat $trainsrc | experiments/subcorpus $(($testoffset + $testsize)) 50000 > $traindev
	cat $trainsrc | experiments/subcorpus 0 $testoffset >> $train
else
	cp $trainsrc $traindev
fi

# Split off development and training data from remaining training data
dev="/tmp/dev.$$"
train="/tmp/train.$$"
devoption=""
if [ "$DEVSIZE" == "0" ] ; then
	cat $traindev | experiments/subcorpus 0 $size > $train
	devoption=" "
	touch $dev
else
	devsize=$DEVSIZE
	if [ -z "$devsize" ] ; then devsize=0 ; fi
	cat $traindev | experiments/subcorpus 0 $devsize > $dev
	cat $traindev | experiments/subcorpus $devsize $size > $train
fi

# Calculate size of training data and md5sums
newsize=`wc -l $train | awk '{print $1}'`
newsize=$(( $newsize / 1000 ))
if [ $newsize -lt $size ] ; then
	echo "Warning: treebank size $newsize is smaller than requested size $size... using maximal size"	
	size=$newsize
fi
testid=`md5sum $test | cut -b 1-8`
devid=`md5sum $dev | cut -b 1-8`
trainid=`md5sum $train | cut -b 1-8`

# Abort if log file already exists
echo "lang=$lang size=$size options=$options devsize=$devsize devoffset=$devoffset testsize=$testsize testoffset=$testoffset"
parses=experiments/results/$type-$lang-$size-$exp-$trainid
if [ -f $parses.log ] ; then
	echo "Log file $parses.log already exists... aborting!"
	exit 1
fi

# Copy test and training data to results directory
trainfile=experiments/results/file-train-$lang-$size-$trainid.conll
devfile=experiments/results/file-dev-$lang-$size-$trainid.conll
testfile=experiments/results/file-test-$lang-$size-$trainid.conll
mstmodel=experiments/results/model₋mst-$lang-$size-$trainid.model
irpmodel=experiments/results/model₋irp-$lang-$size-$trainid.model

if [ ! -f $testfile ] ; then mv $test $testfile ; fi
if [ ! -f $devfile ] ; then mv $dev $devfile ; fi
if [ ! -f $trainfile ] ; then mv $train $trainfile ; fi
if [ -z "$devoption" ] ; then devoption="-train.dev=$devfile" ; fi

# Run command
alloptions="-server -cp dist/MatrixParser.jar:dist/trove.jar $javaopts org.osdtsystem.matrixparser.main.MstTester $train $test $devoption -model.mst=$mstmodel -model.irp=$irpmodel -eval.output1=$parses.mst.conll -eval.output2=$parses.ip.conll $options"
( echo java $alloptions ; java $alloptions 2>&1 ) | tee $parses.log
rm $train 
rm $train

# Create conll evaluation
perl conll/eval.pl -g $test -s $parses.mst.conll > $parses.mst.eval
perl conll/eval.pl -g $test -s $parses.ip.conll > $parses.ip.eval
#dtag -e "load $parses.mst.conll" -e "diff $parses.ip.conll" -e "print /tmp/dtag-diff.${PID}.ps" -e "quit"
#ps2pdf /tmp/dtag-diff.${PID}.ps $parses.mst-diff-ip.pdf
#rm /tmp/dtag-diff.${PID}.ps

